
 
/* --------------------------------------- Include Files ---------------------------------------- */
#include "stm32f10x.h"
#include "common.h"
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

/* ---------------------------------------- Macro Defines --------------------------------------- */


/* -------------------------------------- Data Type Defines ------------------------------------- */


/* --------------------------------- Global Function Prototypes --------------------------------- */
uint8_t Cmn_hexcharToValue(uint8_t val);
void Cmn_byteToHexchar(uint8_t data, uint8_t* result);
void Cmd_int8uToString(uint8_t data, uint8_t* result);
void Cmd_int32uToString(uint32_t data, uint8_t* result);
uint32_t Cmd_isequalString(uint8_t* a, uint8_t* b);
uint32_t Cmd_isequalByteArray(uint8_t* a, uint8_t* b, uint32_t length);
uint32_t replaceCharinString(uint8_t* result, uint8_t* in, uint8_t p_char);

/* --------------------------------- Private Function Prototypes -------------------------------- */


/* -------------------------------------- Global Variables -------------------------------------- */


/* -------------------------------------- Static Variables -------------------------------------- */
uint8_t byteMap[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};


/*************************************************************************************************
 * Procedure: Cmn_hexcharToValue
 * Object: 仅大写
 * Parameters In:
 * - none
 * Parameters Out:
 * - none
 *************************************************************************************************/
uint8_t Cmn_hexcharToValue(uint8_t val)
{
    uint8_t counter;
	for(counter=0; counter<16; counter++){
	    if(byteMap[counter]==val){
		    return counter;
		}
	}
}

/*************************************************************************************************
 * Procedure: Cmn_byteToHexchar
 * Object: 
 * Parameters In:
 * - none
 * Parameters Out:
 * - none
 *************************************************************************************************/
void Cmn_byteToHexchar(uint8_t data, uint8_t* result)
{
    //uint8_t temp[2];
	  *result = byteMap[(data>>4)&0X0F];
	  *(result+1) = byteMap[data&0X0F];
	  return;
}

/*************************************************************************************************
 * Procedure: Cmd_int8uToString
 * Object:
 * Parameters In:
 * - data: 
 * Parameters Out:
 * - none
 *************************************************************************************************/
void Cmd_int8uToString(uint8_t data, uint8_t* result)
{
    uint32_t temp = 0;
	  uint32_t counter = 0;
	  uint8_t n = 0; 
	  uint32_t startflag = 0;
	  uint8_t value = 0;
	
	  for(n=2; n>0; n--){
		    temp = 1;
	      for(counter=n; counter>0; counter--){
		        temp = temp*10;
		    }
		    value = data/temp;
		    data = data%temp;
        if(value!=0){
		        startflag = 1;
		    }
		    if(startflag==1){
		       *(result++) = value + 0X30;
		    }
		}
		*(result++) = data+0X30;
    *(result++) = 0X00;
}

/*************************************************************************************************
 * Procedure: Cmd_int32uToString
 * Object: 
 * Parameters In:
 * - data:  
 * Parameters Out:
 * - none
 *************************************************************************************************/
void Cmd_int32uToString(uint32_t data, uint8_t* result)
{
    uint32_t temp = 0;
	  uint32_t counter = 0;
	  uint8_t n = 0; 
	  uint32_t startflag = 0;
	  uint8_t value = 0;
	
	  for(n=9; n>0; n--){
		    temp = 1;
	      for(counter=n; counter>0; counter--){
		        temp = temp*10;
		    }
		    value = data/temp;
		    data = data%temp;
        if(value!=0){
		        startflag = 1;
		    }
		    if(startflag==1){
		       *(result++) = value + 0X30;
		    }
		}
		*(result++) = data+0X30;
    *(result++) = 0X00;
}

/*************************************************************************************************
 * Procedure: Cmd_isequalString
 * Object: 
 * Parameters In:
 * - data:
 * Parameters Out:
 * - none
 *************************************************************************************************/
uint32_t Cmd_isequalString(uint8_t* a, uint8_t* b)
{
    uint32_t counter = 0;
	
	  for(counter=0; (a[counter]!=0X00)&&(a[counter]!=0X00); counter++){
		    if(a[counter]!=b[counter]){
				    return 0x00;
				}
		}
		
		if(a[counter]!=b[counter]){
		    return 0x00;
		}else{
		    return 0x01;
		}
}

/*************************************************************************************************
 * Procedure: Cmd_isequalByteArray
 * Object:
 * Parameters In:
 * - data:
 * Parameters Out:
 * - none
 *************************************************************************************************/
uint32_t Cmd_isequalByteArray(uint8_t* a, uint8_t* b, uint32_t length)
{
    uint32_t counter = 0;
	
	  for(counter=0; counter<length; counter++){
		    if(a[counter]!=b[counter]){
				    return 0x00;
				}
		}
		
		return 0x01;
}

/*************************************************************************************************
 * Procedure: replaceCharinString
 * Object: 
 * Parameters In:
 * - data:
 * Parameters Out:
 * - none
 *************************************************************************************************/
uint32_t replaceCharinString(uint8_t* result, uint8_t* in, uint8_t p_char)
{
	uint32_t counter = 0;
	uint32_t pt=0;
	
    for(counter=0; (in[counter]!=0x00)&&(counter<2048); counter++){
		    if(in[counter]!=p_char){
					result[pt++] = in[counter];
		    }
		}
		
		result[pt++] = 0x00;
    return pt;
}

/**
  * @brief  LockNumH2A 
  * @param  inbuf, 4B hex   output, 12B asc
  * @retval
  * @brief: 锁号 hex转asc，eg：04 d2 16 2e 01 71->"1234 5678 0369" 
  */
void LockNumH2A(uint8_t *inbuf,uint8_t *outbuf)
{
    uint16_t locknum1,locknum2,locknum3;

    locknum1=inbuf[0];
    locknum1=((locknum1<<8)&0xff00)|inbuf[1];
    locknum2=inbuf[2];
    locknum2=((locknum2<<8)&0xff00)|inbuf[3];
	  locknum3=inbuf[4];
    locknum3=((locknum3<<8)&0xff00)|inbuf[5];
	
    outbuf[0]=(locknum1/1000)+0x30;
    outbuf[1]=(locknum1%1000)/100+0x30;
    outbuf[2]=((locknum1%1000)%100)/10+0x30;
    outbuf[3]=((locknum1%1000)%100)%10+0x30;
    
    outbuf[4]=(locknum2/1000)+0x30;
    outbuf[5]=(locknum2%1000)/100+0x30;
    outbuf[6]=((locknum2%1000)%100)/10+0x30;
    outbuf[7]=((locknum2%1000)%100)%10+0x30;
	
    outbuf[8]=(locknum3/1000)+0x30;
    outbuf[9]=(locknum3%1000)/100+0x30;
    outbuf[10]=((locknum3%1000)%100)/10+0x30;
    outbuf[11]=((locknum3%1000)%100)%10+0x30;	 
    
    outbuf[12]=0x00;
}

/**
  * @brief  PIN_ID_H2A 
  * @param  inbuf,4B hex  output,8B asc   
  * @retval
  * @brief: 锁号 hex转asc，eg0：00 BC 61 4E -> "12345678"
  */
void PIN_ID_H2A(uint8_t *inbuf,uint8_t *outbuf)
{
    uint32_t data;
    
    memcpy(&data,inbuf,4); 
    
    outbuf[0]=(data/10000000)+'0';
    outbuf[1]=(data/1000000)%10+'0';
    outbuf[2]=(data/100000)%10+'0';
    outbuf[3]=(data/10000)%10+'0';    
    outbuf[4]=(data/1000)%10+'0';
    outbuf[5]=(data/100)%10+'0';
    outbuf[6]=(data/10)%10+'0';
    outbuf[7]=(data/1)%10+'0';
}



/**
  * @brief  Hex2Str 
  * @param    
  * @retval
  * @brief: 
  */
void Hex2Str(unsigned char hex) 
{
	char *Str;
	unsigned char x,y,k;
	static char HEXStr[128];

	memset(HEXStr,0,sizeof(HEXStr));
	Str = HEXStr;

	x=hex;
	if(x>99)
	{
	 y=x/100;*Str++=y+'0';
	 x=x-100*y;y=x/10;*Str++=y+'0';
	 x=x-10*y;*Str++=x+'0';
	}
	else if(x>9)
	{
	 y=x/10;*Str++=y+'0';
	 x=x-10*y;*Str++=x+'0';
	}
	else
	{
	 *Str++=x+'0';
	}

	for(k=0;k<3;k++)
	{
	 if(HEXStr[k]==0)
	 {
	   if(k==0){};
	   if(k==1){memmove(HEXStr+2,HEXStr,1);HEXStr[0]=0x30;HEXStr[1]=0x30;}
	   if(k==2){memmove(HEXStr+1,HEXStr,2);HEXStr[0]=0x30; }
	  }
	}
}

/**
  * @brief  Pausedat 
  * @param    
  * @retval
  * @brief: 0x14 0x15 -> 0x07E5
  */
uint16_t Pausedat(uint8_t dat1,uint8_t dat2)
{
	return (dat1 * 100) + dat2;
}